#!/bin/bash
# This script is used to initialize the environment for the project
# Usage: ./init.sh
# Author: LiDong
# Date: 2022-11-08

# 出错退出
set -e
# 获取公网IP
IP=$(curl -s ifconfig.me)

# 使用ip-api.com获取IP地理位置信息
INFO=$(curl -s "http://ip-api.com/json/${IP}")

# 判断是否在中国
if echo "$INFO" | grep -q '"country":"China"'; then
    echo "IP is in China"
    # 如果 REMOTE_CONTAINERS 为 true,则为容器开发环境,进行相关配置
    if [ "$REMOTE_CONTAINERS" = "true" ]; then
        # 容器开发环境,配置容器内部的环境变量
        echo "Configuring environment variables for container development environment"
        # 记录hostname到 data/hostname.txt 如果 data 目录不存在,则创建
        if [ ! -d "data" ]; then
            mkdir data
            chmod 777 data
        fi
        echo "$(hostname)" >data/hostname.txt

        # 配置goproxy
        echo "Configuring goproxy"
        go env -w GO111MODULE=on
        go env -w GOPROXY=https://goproxy.cn,direct

        # 配置npm mirror
        echo "Configuring npm mirror"
        npm config set registry https://registry.npmmirror.com
        yarn config set registry https://registry.npmmirror.com

        # 安装pgit
        echo "Installing pgit"
        curl -o pgit https://gitee.com/gcslaoli/pgit/raw/main/shell/pgit && chmod +x pgit && sudo mv pgit /usr/local/bin

        # 安装cool-tools
        echo "Installing cool-tools ..."
        go install github.com/cool-team-official/cool-admin-go/cool-tools@latest
        # 安装gf
        echo "Installing gf use mirror ..."
        pgit wget -O gf \
            https://github.com/gogf/gf/releases/latest/download/gf_$(go env GOOS)_$(go env GOARCH) &&
            chmod +x gf &&
            ./gf install -y &&
            rm ./gf

    fi

else
    echo "IP is not in China"
    # 如果 REMOTE_CONTAINERS 为 true,则为容器开发环境,进行相关配置
    if [ "$REMOTE_CONTAINERS" = "true" ]; then
        # 容器开发环境,配置容器内部的环境变量
        echo "Configuring environment variables for container development environment"
        # 记录hostname到 data/hostname.txt 如果 data 目录不存在,则创建
        if [ ! -d "data" ]; then
            mkdir data
            chmod 777 data
        fi
        echo "$(hostname)" >data/hostname.txt

        # 安装cool-tools
        echo "Installing cool-tools ..."
        go install github.com/cool-team-official/cool-admin-go/cool-tools@latest
        # 安装gf
        echo "Installing gf use mirror ..."
        wget -O gf \
            https://github.com/gogf/gf/releases/latest/download/gf_$(go env GOOS)_$(go env GOARCH) &&
            chmod +x gf &&
            ./gf install -y &&
            rm ./gf

    fi

fi
